package com.blog.wu.myblog.util;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuyanshen
 * @Date 2018-11-07 5:11 PM
 * @Discription 过滤list的工具类
 */
public class ListUtil {


    /**
     * 根据唯一字段过滤出list中减少的对象的list
     *
     * @param className 对象的className
     * @param key       对象中能保持唯一性的字段名
     * @param list_page 对比的list
     * @param list_db   需要判断的list
     * @return java.util.List<E>
     * @Date 2018/11/7 11:07 PM
     * @Author wuyanshen
     */
    public static <E> List<E> listCompare(String className, String key, List<E> list_page, List<E> list_db) {

        List<E> list_reduce = new ArrayList<>();
        List<String> pageIds = new ArrayList<>();

        try {
            Object ot = Class.forName(className).newInstance();
            Class c = ot.getClass().getSuperclass().getSuperclass();
            Field field = c.getDeclaredField(key);
            field.setAccessible(true);

            //打印传入的每个对象的所有类成员属性值
            for (int i = 0; i < list_page.size(); i++) {
                pageIds.add((String)field.get(list_page.get(i)));
            }

            for (int j = 0; j < list_db.size(); j++) {
                if (!pageIds.contains((String) field.get(list_db.get(j)))) {
                    list_reduce.add(list_db.get(j));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list_reduce;
    }


    public static void main(String[] args) {
        BigDecimal a = new BigDecimal(Double.toString(4.5));
        BigDecimal b = new BigDecimal(Double.toString(1.5));

        System.out.println("a-b="+a.subtract(b));
        System.out.println("a+b="+a.add(b));
        System.out.println("a*b="+a.multiply(b));
        System.out.println("a/b="+a.divide(b));

    }


}
